package com.oba.king.businessprocess.sendordermanage;

import java.util.List;

import com.oba.base.dao.BaseDao;
import com.oba.base.interfaces.IHsqlBuilder;
import com.oba.base.util.Util;
import com.oba.king.controlmanage.proxyproduct.ProxyProductCondition;
import com.oba.king.datalib.Customer;
import com.oba.king.datalib.RecieverInfo;
import com.oba.king.datalib.deliverytype.DeliveryType;
import com.oba.king.datalib.paymenttype.PaymentType;
import com.oba.king.datalib.productinfo.ProductInfo;
import com.oba.king.sysmanager.area.Area;

public class OrderApplyDao extends BaseDao{

	
	public IHsqlBuilder findAll() {
		return getSqlBuilder("from OrderApply o order by o.orderCode");
	}
	
	public IHsqlBuilder findAllCustomer(){
		
		return getSqlBuilder("from Customer c order by c.customerName");
		
	}
	
	public List<Area> findAllArea(){
		
		return find("from Area a order by a.areaName");
		
	}
	public List<RecieverInfo> findAllRecieverInfo(Customer customer){
		IHsqlBuilder sql = getSqlBuilder("from RecieverInfo r");
		sql.add("r.customer = ?",customer);
		sql.addend("order by r.recieverName");
		return find(sql);
		
	}
	public List<DeliveryType> findAllDeliveryType(){
		
		return find("from DeliveryType d order by d.deliveryTypeName");
		
	}
	public List<PaymentType> findAllPaymentType(){
		
		return find("from PaymentType p order by p.paymentTypeName");
		
	}
	
	public IHsqlBuilder findCustomerCondition(SelectCustomerCondition customerCondition){
		IHsqlBuilder sql = getSqlBuilder("from Customer c");
		//sql.add("o.auditingStatus in (1,4)");
		sql.add("c.customerName like ?", customerCondition.getCustomerName(),"%","%");
		sql.add("c.customerCode = ?", customerCondition.getCustomerCode());
		sql.addend("order by c.customerName");
		return sql;
	}
	
	
	
	
	public IHsqlBuilder findProductCondition(List<ProductInfo> list,ProxyProductCondition orderProductCondition,Customer customer) {
		String idsStr ="";
		if(list.size()!=0){
			idsStr = Util.idsFormatByBo(list);		
		} else {
			idsStr = "(0)";
		}
		IHsqlBuilder sql = getSqlBuilder("from ProxyProductDetails p");
		sql.add("p.contract.customer = ?",customer);
		sql.add("p.product.productName like ?", orderProductCondition.getProductName(),"%","%");
		sql.add("p.product.productSerialId = ?", orderProductCondition.getProductSerialId());
		sql.add("p.product.productId not in "+idsStr);
		sql.addend("order by p.contract.contractId");
		return sql;

	}
	
	public IHsqlBuilder findAllProxyProduct(List<ProductInfo> list,Customer customer) {
		
		String idsStr ="";
		if(list.size()!=0){
			idsStr = Util.idsFormatByBo(list);		
		} else {
			idsStr = "(0)";
		}
		IHsqlBuilder sql = getSqlBuilder("from ProxyProductDetails p");
		sql.add("p.contract.customer = ?",customer);
		sql.add("p.product.productId not in "+idsStr);
		sql.addend("order by p.contract.contractId");
		return sql;

	}
	
	public void saveList(List<OrderProductDetails> orderProductList){
		save(orderProductList);
	}
	
	

	public IHsqlBuilder findByOrder(OrderApplyCondition orderApplyCondition) {
		IHsqlBuilder sql = getSqlBuilder("from OrderApply o");
		//sql.add("o.auditingStatus in (1,4)");
		sql.add("o.contractId = ?", orderApplyCondition.getOrderCode());
		sql.add("o.area = ?", orderApplyCondition.getArea());
		sql.add("o.customer = ?", orderApplyCondition.getCustomer());
		sql.add("o.deliverType = ?",orderApplyCondition.getDeliverType());
		sql.add("o.recieverInfo.recieverName like ?",orderApplyCondition.getRecieverName(),"%","%");
		sql.add("o.paymentType = ?",orderApplyCondition.getPaymentType());
		sql.add("o.needInvoice = ?",orderApplyCondition.getNeedInvoice());	
		if(orderApplyCondition!=null&&!(orderApplyCondition.getOrderStatus()==0)){
		sql.add("o.orderStatus = ?",orderApplyCondition.getOrderStatus());
		}
		sql.add("o.applyDate >= ?",orderApplyCondition.getApplyDateStart());
		sql.add("o.applyDate <= ?",orderApplyCondition.getApplyDateEnd());
		sql.addend("order by o.orderCode");
		return sql;

	}
	
	public IHsqlBuilder findNeedAudit() {
		IHsqlBuilder sql = getSqlBuilder("from ProxyContract p");
		sql.add("p.auditingStatus in (1,4)");
		sql.addend("order by p.contractId");
		return sql;
		
	}
	
	private final static String DEL_PRODUCT_DETAILS = "delete from ORDERPRODUCTDETAILS WHERE ORDER_ID = ";
	
	public void deleteOrderApply(OrderApply orderApply){
		jdao.execute(DEL_PRODUCT_DETAILS+orderApply.getOrderId());
		delete(orderApply);
	}
	
}
